package cn.pro.service;

import cn.pro.api.IProUserService;
import cn.pro.dao.ProUserDao;
import cn.pro.dao.ProUserRoleDao;
import cn.pro.entity.ProRole;
import cn.pro.entity.ProUser;
import cn.pro.entity.ProUserRole;
import cn.pro.entity.vo.ProUserVo;
import cn.pro.utils.CheckMsg;
import cn.pro.utils.ObjectUtils;
import cn.pro.utils.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProUserService implements IProUserService {
    @Autowired
    private ProUserDao proUserDao;

    @Autowired
    private ProUserRoleDao proUserRoleDao;

    @Override
    public CheckMsg create(ProUserVo proUser) {
        proUserDao.insert(proUser);
        log.info("名字：{},邮箱：{}",proUser.getName(),proUser.getEmail());
        if (!ObjectUtils.objectCheckIsNull(proUser.getName())){
            CheckMsg.fail().setMesage("用户添加失败");
        }
        return CheckMsg.success().setMesage("用户添加成功");

    }

    @Override
    public PageData<ProUserVo> selectList(ProUser proUser) {
        List<ProUserVo> proUsers = proUserDao.selectList(proUser);
        return new PageData<ProUserVo>().startPage(proUsers, proUser.getPageIndex(), proUser.getPageSize());
    }

    @Override
    public CheckMsg delete(String userId) {
        ProUserVo proUser = proUserDao.selectById(userId);
        if (!org.springframework.util.ObjectUtils.isEmpty(proUser)){
            proUserDao.delById(userId);
            return CheckMsg.success().setMesage("用户删除成功");
        }else {
            return CheckMsg.fail().setMesage("用户删除失败");
        }
    }

    @Override
    public CheckMsg updateUser(ProUser proUser) {
        ProUserVo user = proUserDao.selectById(proUser.getId());
        if (org.springframework.util.ObjectUtils.isEmpty(user)){
            proUser.setUpdateTime(new Date());
            proUserDao.updateById(proUser);
        }
        log.info("名字：{},邮箱：{}",proUser.getName(),proUser.getEmail());
        if (!ObjectUtils.objectCheckIsNull(proUser.getName())){
            return CheckMsg.fail().setMesage("用户修改失败");
        }
        return CheckMsg.success().setMesage("用户修改成功");
    }

    @Override
    public ProUserVo selectById(String userId) {
        ProUserVo proUser = proUserDao.selectById(userId);
        System.out.println("对象："+proUser.toString());
        if (org.springframework.util.ObjectUtils.isEmpty(proUser)){
            return null;
        }
        return proUser;
    }

    @Override
    public CheckMsg bindRoles(String userId, List<String> roleIds) {
        ProUserVo vo = proUserDao.selectById(userId);
        if (!org.springframework.util.ObjectUtils.isEmpty(vo)) {
            for (String rid : roleIds) {
                ProUserRole roleUser = new ProUserRole();
                roleUser.setId(ObjectUtils.couponCodeTwo("r"));
                roleUser.setUserId(userId);
                roleUser.setRoleId(rid);
                roleUser.setCreateTime(new Date());
                roleUser.setUpdateTime(new Date());
                proUserRoleDao.save(roleUser);
            }
            return CheckMsg.success().setMesage("绑定成功");
        }
        return CheckMsg.fail().setMesage("绑定失败");
    }
}
